using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Business.Dto;
using JTZY.Model.Business;
using JTZY.Repository;
using JTZY.Service.Business.IBusinessService;
using System.Collections.Generic;

namespace JTZY.Service.Business
{
    /// <summary>
    /// 地区表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsareaService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsareaService : BaseService<Corecmsarea>, ICorecmsareaService
    {
        /// <summary>
        /// 查询地区表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsareaDto> GetList(CorecmsareaQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Corecmsarea, CorecmsareaDto>(parm);

            return response;
        }

        /// <summary>
        /// 查询地区表树列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<Corecmsarea> GetTreeList(CorecmsareaQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsarea>();


            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToTree(it => it.Children, it => it.ParentId, 0);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsarea GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加地区表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsarea AddCorecmsarea(Corecmsarea model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改地区表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsarea(Corecmsarea model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsarea> QueryExp(CorecmsareaQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsarea>();

            return predicate;
        }

        public string GetAreaFullName(int areaId)
        {
            var alldata = GetAll(true);
            List<Corecmsarea> outAreas = new List<Corecmsarea>();
            GetParentArea(alldata, areaId, outAreas);
            var str = string.Empty;
            if (outAreas.Any())
            {
                outAreas.Reverse();//倒序
                outAreas.ForEach(p => { str += p.Name + " "; });
            }

            return str;
        }
        private void GetParentArea(List<Corecmsarea> cacheAreas, int id, List<Corecmsarea> outAreas)
        {
            //获取当前级别
            var model = cacheAreas.FirstOrDefault(p => p.Id == id);
            if (model != null)
            {
                if (outAreas.All(p => p.Id != model.Id)) outAreas.Add(model);
                //获取父级
                var parentModel = cacheAreas.Find(p => p.Id == model.ParentId);
                if (parentModel != null)
                {
                    if (outAreas.All(p => p.Id != parentModel.Id)) outAreas.Add(parentModel);
                    if (parentModel.ParentId != 0)
                    {
                        //上面还有节点
                        var parentParentModel = cacheAreas.Find(p => p.Id == parentModel.ParentId);
                        if (parentParentModel != null && outAreas.All(p => p.Id != parentParentModel.Id)) outAreas.Add(parentParentModel);
                    }
                }
            }
        }
    }
}